|
1
|
|
|
/** |
|
2
|
|
|
* Test for class CardDeck. |
|
3
|
|
|
*/ |
|
4
|
|
|
"use strict"; |
|
5
|
|
|
|
|
6
|
|
|
/* global describe it */ |
|
7
|
|
|
|
|
8
|
|
|
const assert = require("assert"); |
|
9
|
|
|
const CardDeck = require("../../src/cardDeck/cardDeck"); |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Compare two integers. |
|
15
|
|
|
*/ |
|
16
|
|
|
function compareInteger(a, b) { |
|
17
|
|
|
return a - b; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Testsuite |
|
24
|
|
|
*/ |
|
25
|
|
|
describe("Check CardDeck", function() { |
|
26
|
|
|
describe("use default card deck", function() { |
|
27
|
|
|
it("should be 104 cards", function() { |
|
28
|
|
|
let deck = new CardDeck(); |
|
29
|
|
|
let numberOfCards = deck.getNumberOfCards(); |
|
30
|
|
|
|
|
31
|
|
|
assert.equal(numberOfCards, 104); |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
it("id should be between 0 and 103", function() { |
|
35
|
|
|
let deck = new CardDeck(); |
|
36
|
|
|
let cards = deck.showAllCardsById(); |
|
37
|
|
|
|
|
38
|
|
|
assert.equal(cards.length, 104); |
|
39
|
|
|
assert.equal(cards[0], 0); |
|
40
|
|
|
assert.equal(cards[51], 51); |
|
41
|
|
|
assert.equal(cards[52], 0); |
|
42
|
|
|
assert.equal(cards[103], 51); |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
it("shuffle it", function() { |
|
46
|
|
|
let deck = new CardDeck(); |
|
47
|
|
|
let numberOfCards; |
|
48
|
|
|
|
|
49
|
|
|
deck.shuffle(); |
|
50
|
|
|
numberOfCards = deck.getNumberOfCards(); |
|
51
|
|
|
|
|
52
|
|
|
assert.equal(numberOfCards, 104); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
it("get all cards, one by one", function() { |
|
56
|
|
|
let deck = new CardDeck(); |
|
57
|
|
|
let allCards = deck.showAllCardsById(); |
|
58
|
|
|
let cardId; |
|
59
|
|
|
let shuffledCards = []; |
|
60
|
|
|
let equal; |
|
61
|
|
|
|
|
62
|
|
|
deck.shuffle(); |
|
63
|
|
|
while ((cardId = deck.getCard()) !== undefined) { |
|
64
|
|
|
shuffledCards.push(cardId); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
allCards.sort(compareInteger); |
|
68
|
|
|
shuffledCards.sort(compareInteger); |
|
69
|
|
|
|
|
70
|
|
|
equal = shuffledCards.length == allCards.length && |
|
71
|
|
|
shuffledCards.every((element, index) => { |
|
72
|
|
|
return element === allCards[index]; |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
assert.equal(equal, true); |
|
76
|
|
|
}); |
|
77
|
|
|
}); |
|
78
|
|
|
}); |
|
79
|
|
|
|